package com.qf.sale.controller;

import com.qf.lw.pojo.CustomerPojo;
import com.qf.lw.pojo.GoodsPojo;
import com.qf.lw.pojo.OrderPojo;
import com.qf.sale.timer.OrderTimerTask;
import com.qf.sale.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;

@Controller
public class OrderController {
    @Autowired
    IOrderService orderService;


    public static Map<String, Timer> map = new HashMap<>();

    //创建订单点击结算
    @RequestMapping("createOrder")
    public ModelAndView createOrder(String[] goodsIds, HttpSession session, int addressId) {

        ModelAndView mv = new ModelAndView();
        //如果，没有勾选商品，提示勾选
        if (null == goodsIds) {
            mv.addObject("errorMsg", "没有勾选商品,请勾选");
            mv.setViewName("shopcart");
            return mv;
        }
        if('0'==addressId){
            mv.addObject("errorMsg", "请选择收货地址");
            mv.setViewName("shopcart");
            return mv;
        }
        //是谁的订单？
        CustomerPojo customerPojo = (CustomerPojo) session.getAttribute("customer");

        //如果没有找到顾客就返回重新登陆
        if (null == customerPojo) {
            mv.setViewName("login");
            return mv;
        }
        //如果有就进行下一步
        //获取购物车 ; 我们自己定义的业务; 正常的业务是 如果直接结算一个商品,是不需要添加到购物车的
        Map<String, GoodsPojo> shopCar = (Map<String, GoodsPojo>) session.getAttribute("shopCar");
        //如果购物车不存在怎返回index页面去加入购物车
        if (null == shopCar) {
            mv.setViewName("index");
            return mv;//购物车没有东西
        }
        //创建订单以及订单详情

        OrderPojo orderPojo = orderService.create(customerPojo.getAid(), goodsIds, shopCar, addressId);


        //设置一个订单超时的时间点;30分钟后订单自动关闭
        //1: 订单创建成功以后,我们需要设置一个计时器;计时器30s;
        //2: 30s以后,订单失效; 就是修改订单的状态为 10; 就代表当前定时已经失效了;
        //3: 如果用户在指定的时间内支付成功;必须要取消这个计时器;
        Timer timer = new Timer();
        OrderTimerTask ott = new OrderTimerTask(orderService, timer, orderPojo.getOid());
        timer.schedule(ott, 3000);
        map.put(orderPojo.getOid(), timer);


        session.setAttribute("shopCar", shopCar);
        mv.addObject("orderPojo", orderPojo);
        mv.setViewName("apay");
        return mv;
    }

    //假如支付成功，一定要取消定时器
    @RequestMapping("apay")
    public String apay(String oid) {
        boolean bl = orderService.apaySuccess(oid);
        map.get(oid).cancel();
        map.remove(oid);//从map里面移除这个计时器
        return "success";
    }

    @RequestMapping("show")
    public ModelAndView orderShow(HttpSession session,int aid) {
        ModelAndView mv = new ModelAndView();

        CustomerPojo customerPojo=orderService.ordershow(aid);
        mv.addObject("customerPojo",customerPojo);
        mv.setViewName("ordershow");
        return mv;
    }

}
